﻿Imports System.Windows.Forms
Imports WeifenLuo.WinFormsUI
Imports VS2005Extender
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.Xml.Serialization
Imports System.Threading
Imports GisMapF
Public Class frmAnalyseSystem


    ''' <summary>
    ''' 当前项目总的控制
    ''' </summary>
    ''' <remarks></remarks>
    Public NowProj As CAermod.ProjSet

    Public frmRectable As New frmRectableResult '计算结果最大值输出窗口
    Public frmGridResult As New frmGridResult  '网格点
    'Public frmPost As New frmPostFile
    Public MaxiFileWindow As New frmMaxiFileResult '超过限值文件分析
    Public frmProjectResult As New frmProjectResult   '项目管理器窗口
    Public frmResultControl As New frmResultControl '结果控制窗口


    Public frmGis_Map As frmGisResult  '小付的GIS窗口
    Public m_prop As GisMapF.PropFormV2
    Public m_tree As GisMapF.TreeFormV2



    ''三维部分的窗口
    'Public m_vtkTree As Fu3DEngine.TreeForm
    'Public m_vtkProp As Fu3DEngine.PropertyForm
    'Public m_vtkRenderForm As frmVtkRender





    Private m_choose As Cursor = SetCursor(SetBitmap("choose"), New PointF(0, 0))
    Private m_move As Cursor = SetCursor(SetBitmap("move"), New PointF(0, 0))
    Private m_zoom_out As Cursor = SetCursor(SetBitmap("zoom-out"), New PointF(0, 0))

    ' Private m_zoom_out As Cursor = SetCursor(b, New PointF(0, 0))
    Private m_zoom_in As Cursor = SetCursor(SetBitmap("zoom-in"), New PointF(0, 0))

    Private helper As New SetupHelper

    Private Sub frmAnalyseSystem_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        'Me.NowProj.IsSavedResult = True
        Me.frmGis_Map.m_manager.Save("GisProj") '统一用GisProj这个文件名来保存文件
        Me.frmGis_Map.m_manager.UnloadAllLayer()

        '将用户设置好的属性序列化在安装目录
        '保存主项目文件------------------------------
        Dim fileStr As Stream
        fileStr = File.Open(PublicVal.AermodProject0.GetProjectPath & "\DrawPropty.dwp", FileMode.Create)
        Dim formatter As IFormatter
        formatter = CType(New BinaryFormatter, IFormatter)
        formatter.Serialize(fileStr, PublicVal.CommonDrawProperty)
        fileStr.Dispose()
        ' All done
        fileStr.Close()

        Try
            fileStr = File.Open(PublicVal.AermodProject0.GetProjectPath & "\DrawPropty.dwp", FileMode.Open)

            formatter = CType(New BinaryFormatter, IFormatter)
            fileStr.Seek(0, SeekOrigin.Begin)
            Dim obj As Object = formatter.Deserialize(fileStr)
            ' All done
            fileStr.Close()
        Catch ex As Exception
            fileStr.Close()
            MsgBox("序列化公共属性时出现意外错误!")
        End Try
    End Sub


    '关于激活事件换窗口的问题，下面的函数已经不在使用
    Protected Sub OnGisActivated(ByVal sender As System.Object, ByVal e As System.EventArgs)

        'm_vtkProp.Hide()
        'm_vtkTree.Hide()

        m_tree.Show()
        m_prop.Show()



    End Sub
    Protected Sub OnVtkActivated(ByVal sender As System.Object, ByVal e As System.EventArgs)
        m_tree.Hide()
        m_prop.Hide()

        'm_vtkProp.Show()
        'm_vtkTree.Show()



    End Sub

    Private Sub frmAnalyseSystem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fileStr As Stream
        Try
            If My.Computer.FileSystem.FileExists(PublicVal.AermodProject0.GetProjectPath & "\DrawPropty.dwp") Then
                fileStr = File.Open(PublicVal.AermodProject0.GetProjectPath & "\DrawPropty.dwp", FileMode.Open)
                Dim formatter As IFormatter
                formatter = CType(New BinaryFormatter, IFormatter)
                fileStr.Seek(0, SeekOrigin.Begin)
                Dim obj As Object = formatter.Deserialize(fileStr)
                ' All done
                fileStr.Close()
                PublicVal.CommonDrawProperty = obj
            End If
        Catch ex As Exception
            fileStr.Close()
            MsgBox("反序列化开公共属性时出现意外错误!")
        End Try

        Me.helper = New SetupHelper
        PublicVal.CommonDrawProperty._Contour.LdescProperty = "等值线"
        PublicVal.CommonDrawProperty._grd.LdescProperty = "网格"
        PublicVal.CommonDrawProperty._axis.LdescProperty = "坐标轴"
        PublicVal.CommonDrawProperty._build.LdescProperty = "建筑物"
        PublicVal.CommonDrawProperty._ppoint.LdescProperty = "点源"
        PublicVal.CommonDrawProperty._cube.LdescProperty = "体源"
        PublicVal.CommonDrawProperty._rect.LdescProperty = "矩形面源"
        PublicVal.CommonDrawProperty._poly.LdescProperty = "多边形面源"
        PublicVal.CommonDrawProperty._circle.LdescProperty = "圆形面源"
        PublicVal.CommonDrawProperty._exp.LdescProperty = "敞口源"
        PublicVal.CommonDrawProperty._cpoint.LdescProperty = "关心点"
        helper.AddLayer(PublicVal.CommonDrawProperty._Contour)
        helper.AddLayer(PublicVal.CommonDrawProperty._grd)
        helper.AddLayer(PublicVal.CommonDrawProperty._axis)
        helper.AddLayer(PublicVal.CommonDrawProperty._build)
        helper.AddLayer(PublicVal.CommonDrawProperty._ppoint)
        helper.AddLayer(PublicVal.CommonDrawProperty._cube)
        helper.AddLayer(PublicVal.CommonDrawProperty._rect)
        helper.AddLayer(PublicVal.CommonDrawProperty._poly)
        helper.AddLayer(PublicVal.CommonDrawProperty._circle)
        helper.AddLayer(PublicVal.CommonDrawProperty._exp)
        helper.AddLayer(PublicVal.CommonDrawProperty._cpoint)


       


        Me.NowProj = New CAermod.ProjSet   '初始化计算结果方案
        Dim Path As String = System.IO.Path.GetDirectoryName(AermodProject0.SaveName) & "\" & CAermod.GetFileName(AermodProject0.SaveName) & "\Aermod\" & AermodProject0.ProjSetCollection.Item(0).ID
        Me.frmGis_Map = New frmGisResult(Path)
        m_tree = New TreeFormV2(Me.frmGis_Map.m_manager)
        m_prop = New PropFormV2(Me.frmGis_Map.m_manager)

        Me.m_tree.Text = "图层管理"
        AddHandler m_tree.LayerSelected, AddressOf m_prop.OnLayerSelected

        'GIS窗口的显示
        '显示或隐藏窗口
        Me.frmRectable.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document) '显示各点高值窗口
        Me.frmGridResult.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document) '显示网格数据窗口
        Me.frmGis_Map.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document) '显示GIS窗口
        Me.frmRectable.Visible = False
        '








        ''三维方向窗口初始化
        'm_vtkProp = New Fu3DEngine.PropertyForm()
        'm_vtkProp.Text = "三维属性编辑"
        'm_vtkProp.HideOnClose = True


        'm_vtkTree = New Fu3DEngine.TreeForm(m_vtkProp)
        'm_vtkTree.Text = "三维节点管理"
        'm_vtkTree.HideOnClose = True


        'm_vtkRenderForm = New frmVtkRender()



        ''关于三维窗口的创建与隐藏
        'Me.m_vtkRenderForm.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document)



        '下面这段代码已经被废弃了，为了解决三维弹出后一切清零的问题

        '关于窗口激活的事件
        'AddHandler frmGis_Map.Activated, AddressOf Me.OnGisActivated
        'AddHandler m_vtkRenderForm.Activated, AddressOf Me.OnVtkActivated









        Me.Text = My.Application.Info.ProductName & My.Application.Info.Version.ToString.Substring(0, 3)

        ' Set DockPanel properties
        DockPanel.ActiveAutoHideContent = Nothing
        DockPanel.Parent = Me
        DockPanel.SuspendLayout(True)

        DockPanel.ResumeLayout(True, True)

        CreateBasicLayout()
        '小阳添加 界面设置
        '填充office2003 样式
        ToolStripManager.Renderer = New ToolStripProfessionalRenderer(New Custominterfacecolor())
        MenuStrip1.BackColor = Color.FromArgb(190, 214, 249)

        '设置工具档上的下拉框

        Me.frmResultControl.cmbCon.SelectedIndex = 0
        Me.frmGis_Map.CreateGis(False)

        'Me.frmProjectResult.ChangeProj()
        Me.frmRectable.Activate()

        '关闭时隐藏的窗体
        Me.frmProjectResult.HideOnClose = True
        Me.frmResultControl.HideOnClose = True
        'Me.m_vtkProp.HideOnClose = True
        'Me.m_vtkTree.HideOnClose = True

        m_tree.HideOnClose = True
        m_prop.HideOnClose = True
    End Sub

    '创建窗口，并改变菜单的状态
    Private Sub CreateBasicLayout()

        '数据分析窗口
        'Me.ResultDataWindow.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document)
        'Me.ResultDataWindow.Hide()

        'Me.MaxResultWindow.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document)
        'Me.MaxResultWindow.Hide()

        'Me.MaxiFileWindow.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document)
        'MaxiFileWindow.Hide()

        'Me.PostFileWinow.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document)
        'PostFileWinow.Hide()

        'Me.DockPanel.DockLeftPortion = 250
        'OutWindow.Show(DockPanel, Docking.DockState.DockBottom)
        'Me.DockPanel.DockRightPortion = 250
        '显示方案设置窗口
        frmProjectResult.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.DockLeft)
        '显示项目窗口
        Me.frmResultControl.Show(frmProjectResult.DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.DockLeft)
        Me.frmResultControl.DockTo(frmProjectResult.Pane, DockStyle.Bottom, 0)

        m_tree.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.DockRight)
        m_prop.Show(m_tree.DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.DockBottom)
        m_prop.DockTo(m_tree.Pane, DockStyle.Bottom, 0)


        '三维相关代码

        'm_vtkTree.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.DockRight)
        'm_vtkProp.Show(m_tree.DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.DockBottom)

        'm_vtkProp.DockTo(m_vtkTree.Pane, DockStyle.Bottom, 0)




        '第一次更新显示，求得默认位置的显示
        m_tree.Hide()
        m_prop.Hide()
        'm_vtkTree.Hide()
        'm_vtkProp.Hide()


        '显示GIS
        'frmGis_Map.Show(DockPanel, Docking.DockState.Document)


        '三维窗口布局，注意默认状况下，树和属性是不显示的
        'm_vtkRenderForm.Show(DockPanel, Docking.DockState.Document)



    End Sub







    Private Sub cmbCon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ToolStripButtonZoomIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButtonZoomIn.Click
        Me.frmGis_Map.m_manager.SetMZoomIn(Me.m_zoom_in)
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        frmGis_Map.m_manager.SetToExtent()
    End Sub

    Private Sub ToolStripButtonZoomOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButtonZoomOut.Click
        Me.frmGis_Map.m_manager.SetMZoomOut(Me.m_zoom_out)
    End Sub
    ''' <summary>
    ''' 返回函数
    ''' </summary>
    ''' <param name="flag"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetBitmap(ByVal flag As String) As Bitmap

        Dim m_bitmap As Bitmap
        If flag = "choose" Then
            Try
                m_bitmap = Bitmap.FromFile(My.Application.Info.DirectoryPath + "\mouse\choose.png")
            Catch ex As Exception
                MsgBox("鼠标指针丢失，重新安装软件可解决问题", MsgBoxStyle.OkOnly, "提示")
            End Try
        ElseIf flag = "move" Then
            Try
                m_bitmap = Bitmap.FromFile(My.Application.Info.DirectoryPath + "\mouse\move.png")
            Catch ex As Exception
                MsgBox("鼠标指针丢失，重新安装软件可解决问题", MsgBoxStyle.OkOnly, "提示")
            End Try
        ElseIf flag = "zoom-out" Then
            Try
                m_bitmap = Bitmap.FromFile(My.Application.Info.DirectoryPath + "\mouse\zoom-out.png")
            Catch ex As Exception
                MsgBox("鼠标指针丢失，重新安装软件可解决问题", MsgBoxStyle.OkOnly, "提示")
            End Try
        ElseIf flag = "zoom-in" Then
            Try
                m_bitmap = Bitmap.FromFile(My.Application.Info.DirectoryPath + "\mouse\zoom-in.png")
                '  m_bitmap = New Bitmap(Me.GetType, "zoom-in.ico")
            Catch ex As Exception
                MsgBox("鼠标指针丢失，重新安装软件可解决问题", MsgBoxStyle.OkOnly, "提示")
            End Try
        End If


        Return m_bitmap

    End Function
    ''' <summary>
    ''' 使用任何图片做鼠标指针
    ''' </summary>
    ''' <param name="CCursor"></param>
    ''' <param name="Point"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetCursor(ByVal CCursor As Bitmap, ByVal Point As System.Drawing.PointF) As Cursor
        Dim hotX As Integer = Point.X
        Dim hoty As Integer = Point.Y

        Dim myNewCursor As Bitmap = New Bitmap(Cursor.Size.Width * 2 - hotX, Cursor.Size.Height * 2 - hoty)

        Dim g As Graphics = Graphics.FromImage(myNewCursor)
        g.Clear(Color.FromArgb(0, 0, 0, 0))
        g.DrawImage(CCursor, CCursor.Size.Width - hotX, CCursor.Size.Height - hoty, CCursor.Size.Width, CCursor.Size.Height)

        Return New Cursor(myNewCursor.GetHicon())
        'Cursor = New Cursor(myNewCursor.GetHicon())
        g.Dispose()
        myNewCursor.Dispose()
        CCursor.Dispose()

    End Function

    Private Sub ToolStripButtonArrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButtonArrow.Click
        Me.frmGis_Map.m_manager.SetMSelect(Cursors.Arrow)
    End Sub

    Private Sub ToolStripButtonMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButtonMove.Click
        Me.frmGis_Map.m_manager.SetMHand()
    End Sub

    Private Sub 保存图片ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存图片ToolStripMenuItem.Click
        Dim m_ImageOutputFormV2 As New ImageOutputFormV2(Me.frmGis_Map.m_manager)
        m_ImageOutputFormV2.ShowDialog()
    End Sub



    Public fucon As New DrawContour.Contours



    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click

        If Fu3DEngine.FuEngine.GetEngine().GetScene("terrainscene") IsNot Nothing Then

            Dim imgout As New Fu3DEngine.ImageOutPutForm(Fu3DEngine.FuEngine.GetEngine().GetScene("terrainscene"))

            imgout.ShowDialog()

        End If

    End Sub


    Private Sub DockPanel_ActiveDocumentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DockPanel.ActiveDocumentChanged


        '根据不同的当前文档，提供不同的窗体结构
        If DockPanel.ActiveDocument IsNot Nothing Then

            If TypeOf (DockPanel.ActiveDocument) Is frmVtkRender Then

                ' MessageBox.Show("3d!")
                'If m_tree Is Nothing Or m_prop Is Nothing Or m_vtkProp Is Nothing Or m_vtkTree Is Nothing Then

                '    Return

                'End If

                'm_tree.Hide()
                'm_prop.Hide()

                'm_vtkProp.Show()
                'm_vtkTree.Show()
                '小齐加
                Me.Tool3DManage.Checked = True
                Me.Tool3DProperty.Checked = True

            End If



            If TypeOf (DockPanel.ActiveDocument) Is frmGisResult Then

                'If m_tree Is Nothing Or m_prop Is Nothing Or m_vtkProp Is Nothing Or m_vtkTree Is Nothing Then
                '    Return
                'End If

                'm_vtkProp.Hide()
                'm_vtkTree.Hide()

                m_tree.Show()
                m_prop.Show()
                '小齐加
                Me.ToolLayerManage.Checked = True
                Me.ToolLayerProperty.Checked = True


            End If




        End If







    End Sub

    Private Sub 通用表现设置ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 通用表现设置ToolStripMenuItem.Click

        

        Dim psf As New PropSetupForm(helper)
        If psf.ShowDialog() = Windows.Forms.DialogResult.OK Then
            
        End If


    End Sub


    Private Sub ToolProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolProject.Click
        If Me.ToolProject.CheckState = CheckState.Unchecked Then
            Me.frmProjectResult.Show()
            Me.ToolProject.Checked = True
        Else
            Me.frmProjectResult.Hide()
            Me.ToolProject.Checked = False
        End If
    End Sub

    Private Sub ToolResultControl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolResultControl.Click
        If Me.ToolResultControl.CheckState = CheckState.Unchecked Then
            frmResultControl.Show()
            Me.ToolResultControl.Checked = True
        Else
            frmResultControl.Hide()
            Me.ToolResultControl.Checked = False
        End If
    End Sub

    Private Sub Tool3DManage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool3DManage.Click
        If Me.Tool3DManage.CheckState = CheckState.Unchecked Then
            'm_vtkTree.Show()
            Me.Tool3DManage.Checked = True
        Else
            'm_vtkTree.Hide()
            Me.Tool3DManage.Checked = False
        End If
    End Sub

    Private Sub Tool3DProperty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tool3DProperty.Click
        If Me.Tool3DProperty.CheckState = CheckState.Unchecked Then
            'Me.m_vtkProp.Show()
            Me.Tool3DProperty.Checked = True
        Else
            'Me.m_vtkProp.Hide()
            Me.Tool3DProperty.Checked = False
        End If
    End Sub

    Private Sub ToolReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolReset.Click
        CreateBasicLayout()

        Me.frmGis_Map.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document) 'Gis
        'Me.m_vtkRenderForm.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document) '3D
        Me.frmRectable.Show(DockPanel, WeifenLuo.WinFormsUI.Docking.DockState.Document) '关心点

        Me.ToolProject.Checked = True
        Me.ToolResultControl.Checked = True
        Me.Tool3DManage.Checked = True
        Me.Tool3DProperty.Checked = True
        Me.ToolLayerManage.Checked = True
        Me.ToolLayerProperty.Checked = True
    End Sub

    Private Sub ToolView_DropDownOpened(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolView.DropDownOpened
        If Me.frmProjectResult.IsHidden Then
            Me.ToolProject.Checked = False
        End If
        If Me.frmResultControl.IsHidden Then
            Me.ToolResultControl.Checked = False
        End If

        'If Me.m_vtkRenderForm.Focused Then
        '    Me.ToolLayerManage.Visible = False
        '    Me.ToolLayerProperty.Visible = False

        '    Me.Tool3DManage.Visible = True
        '    Me.Tool3DProperty.Visible = True
        'End If
        'If Me.frmGis_Map.Focused Then
        '    Me.ToolLayerManage.Visible = True
        '    Me.ToolLayerProperty.Visible = True

        '    Me.Tool3DManage.Visible = False
        '    Me.Tool3DProperty.Visible = False
        'End If

        'If Me.m_vtkTree.IsHidden Then
        '    Me.Tool3DManage.Checked = False
        'End If
        'If Me.m_vtkProp.IsHidden Then
        '    Me.Tool3DProperty.Checked = False
        'End If
        If Me.m_tree.IsHidden Then
            Me.ToolLayerManage.Checked = False
        End If
        If Me.m_prop.IsHidden Then
            Me.ToolLayerProperty.Checked = False
        End If
    End Sub

    Private Sub ToolLayerManage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolLayerManage.Click
        If Me.ToolLayerManage.CheckState = CheckState.Unchecked Then
            Me.m_tree.Show()
            Me.ToolLayerManage.Checked = True
        Else
            Me.m_tree.Hide()
            Me.ToolLayerManage.Checked = False
        End If
    End Sub

    Private Sub ToolLayerProperty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolLayerProperty.Click
        If Me.ToolLayerProperty.CheckState = CheckState.Unchecked Then
            Me.m_prop.Show()
            Me.ToolLayerProperty.Checked = True
        Else
            Me.m_prop.Hide()
            Me.ToolLayerProperty.Checked = False
        End If
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click, 将公共属性用于当前绘图ToolStripMenuItem.Click
        '将公共设置用于当前绘图
        Me.frmGis_Map.UseCommonGisSet()
    End Sub

End Class